package com.msmall.community.keyword.preparer;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.tiles.AttributeContext;
import org.apache.tiles.context.TilesRequestContext;
import org.apache.tiles.preparer.ViewPreparer;

import com.msmall.base.cache.CacheFactory;
import com.msmall.base.util.ConstantDB;
import com.msmall.base.util.Constants;
import com.msmall.community.keyword.model.Keyword;

public class KeywordCookbookPreparer implements ViewPreparer{
	
	private static final Log log = LogFactory.getLog(KeywordCookbookPreparer.class);
	
	public void execute(TilesRequestContext tilesContext, AttributeContext context) {
		try{
			HttpServletRequest request = (HttpServletRequest) tilesContext.getRequest();
			Short keywordType = 0;
			if(context.getAttribute("keywordType") != null){
				keywordType = (Short) context.getAttribute("keywordType").getValue();
			}
			Map<Short, List<Keyword>> maps = (Map<Short, List<Keyword>>) CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_KEYWORD);
			if(keywordType == null || keywordType == 0){
				keywordType = ConstantDB.KEYWORD_TYPE_COOKBOOK_MOTHER_BABY;
			}
			request.setAttribute("babyMotherCookbookKeywords", maps.get(keywordType));
		}catch(Exception e){
			log.debug(e);
		}
	}
}
